package com.tuanchau.englishtooltip;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tuanchau.englishtooltip.responddata.Responder;

@SuppressWarnings("serial")
public class EnglishToolTipServerServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		doProcess(req, resp);

	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		doProcess(req, resp);
	}

	private void doProcess(HttpServletRequest req, HttpServletResponse resp) {
		resp.setContentType("text/plain; charset=utf-8");
		String respord = "";
		try {
			int action = Integer.parseInt(req.getParameter(Global.REQ_ACTION));
			switch (action) {
			case Global.ACTION_ADD:
				respord = Responder.doOnActionAdd(req);
				break;
			case Global.ACTION_ADDS:
				respord = Responder.doOnActionAdds(req, resp.getWriter());
				break;
			case Global.ACTION_GET_WITH_WORD:
				try {
				respord = Responder.doOnActionGetWithWord(req);
				}
				catch(Exception e) {
					resp.getWriter().println("do con heo");
				}
				break;
//			case Global.ACTION_GET_WITH_WORD_TYPE:
//				respord = Responder.doOnActionGetWithWordAndType(req);
//				break;
			case Global.ACTION_RESET_DATABASE:
				respord = Responder.doResetDatabase(req, resp);
				break;
			default:
				respord = "Unknown action";
			}
		} catch (NumberFormatException e) {
			respord = "Action must be integer";
		} catch (Exception e) {
			respord = "Unknown Exception";
		}
		finally {
			try {
				resp.getWriter().print(respord);
			} catch (IOException e1) {

				e1.printStackTrace();
			}
		}
	}
	
	
}
